﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCPCommServer
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime now;
            String strDateLine;
            Encoding ASCII = Encoding.ASCII;
            // 在端口 13 进行侦听
            TcpListener tcpl = new TcpListener(13);
            tcpl.Start();
            Console.WriteLine("服务器地址为" + tcpl.LocalEndpoint);
            Console.WriteLine("正在等待客户进行连接");
            Console.WriteLine("请按 Ctrl+c 退出...");
            while (true)
            {
                // 接收会阻塞，直到有人连接上
                Socket s = tcpl.AcceptSocket();
                // 获取当前的日期和时间并将它连接成一个字符串
                now = DateTime.Now;
                strDateLine = now.ToShortDateString() + " " +
                now.ToLongTimeString();
                // 将该字符串转换成一个字节数组并发送它
                Byte[] byteDateLine =
                ASCII.GetBytes(strDateLine.ToCharArray());
                s.Send(byteDateLine, byteDateLine.Length, 0);
                Console.WriteLine("发送" + strDateLine);
            }

        }
    }
}
